package com.jack.collections;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

public class ConcurrentHashMapTest {

    static  volatile int NUM = 0;


    public static void main(String[] args) {
        Collections.synchronizedList(new ArrayList<>());
        new CopyOnWriteArrayList<>();
        int iter = 100000;
        Hashtable<Integer, Integer> hashtable = new Hashtable<>();
        long start = System.currentTimeMillis();
        for (int i = 0; i < iter; i++) {
            hashtable.put(i,i);
        }
        System.out.println("hashtable 耗时：" + ( System.currentTimeMillis() - start));

        ConcurrentHashMap<Integer,Integer> concurrentHashMap = new ConcurrentHashMap();
        start = System.currentTimeMillis();
        for (int i = 0; i < iter; i++) {
            concurrentHashMap.put(i, i );
        }
        System.out.println("concurrentHashmap 耗时：" + (System.currentTimeMillis() - start));


    }


//    static class MyThread implements Runnable{
//
//        CountDownLatch latch;
//
//        static ConcurrentHashMap<Integer, Integer> concurrentHashMap = new ConcurrentHashMap();
//
//        int num;
//
//        public MyThread(CountDownLatch latch, int num) {
//            this.latch = latch;
//            this.num = num;
//        }
//
//        @Override
//        public void run() {
//            try {
//                latch.await();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//
//            for (int i = 1; i < 1001; i++) {
//                concurrentHashMap.put(i * num, i);
//            }
//            NUM = concurrentHashMap.size();
//        }
//    }

//    static class HashTableThread implements Runnable{
//
//        CountDownLatch latch;
//
//        static Hashtable<Integer, Integer> table = new Hashtable();
//
//        int num;
//
//        public HashTableThread(CountDownLatch latch, int num) {
//            this.latch = latch;
//            this.num = num;
//        }
//
//        @Override
//        public void run() {
//            try {
//                latch.await();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            for (int i = 1; i < 1001; i++) {
//                table.put(i * num, i);
//            }
//            System.out.println(table.size());
//            NUM = table.size();
//        }
//    }
}
